home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 1 / Gekikoh Dennoh Club Vol. 1 (Japan).7z / Gekikoh Dennoh Club Vol. 1 (Japan) (Track 1).bin / kowin / archive / apl / tvv132.lzh / readfile.c < prev    next >
C/C++ Source or Header  |  1995-12-27  |  4KB  |  193 lines

  1. /*
  2.     MicroView.win Copyright 1995 小笠原博之
  3.     oga@dgw.yz.yamagata-u.ac.jp
  4. */
  5.  
  6. #include    "microview.h"
  7.  
  8. #define        Print(msg)    ConsoleAutoPrint(msg)
  9. #define        FOFF        1024
  10. #define        Ctrl(a)        ((a)&0x1f)
  11.  
  12. static unsigned char    _KanjiC[]= {
  13.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  14.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  15.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  16.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  17.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  18.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  19.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  20.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  21.  
  22.     1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
  23.     1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
  24.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  25.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  26.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  27.     0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  28.     1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
  29.     1,1,1,1,0,0,0,0, 0,0,0,0,0,0,0,0,
  30. };
  31.  
  32. static unsigned char *
  33. skipnum( ptr )
  34. unsigned char    *ptr;
  35. {
  36.     for(; *ptr >= '0' && *ptr <= '9' ; ptr++ );
  37.     return    ptr;
  38. }
  39.  
  40. static int
  41. nn_numstr( ptr )
  42. unsigned char    *ptr;
  43. {
  44.     int    i= 0;
  45.     for(; *ptr >= '0' && *ptr <= '9' ; i*= 10, i+= (*ptr++&15) );
  46.     return    i;
  47. }
  48.  
  49. static unsigned char *
  50. MakeBuf( bp, size )
  51. T_BUFP    *bp;
  52. {
  53.     unsigned char    *ptr= (unsigned char*)(bp->lp)+ FOFF,
  54.             *last= ptr+ size,
  55.             *str= bp->fbuf,
  56.             **lp= bp->lp;
  57.     int    font= 12, attr= AttrDefault, option= 0;
  58.     int    line;
  59.     for( line= 0 ; ptr < last ; ){
  60.         unsigned char    kanji= 0;
  61.         int    col;
  62.         if( *ptr == Ctrl('F') ){
  63.             bp->font_z= nn_numstr( ptr+1 );
  64.             goto    __Cont;
  65.         }else if( *ptr == Ctrl('A') ){
  66.             bp->attr= nn_numstr( ptr+1 );
  67.             bp->backattr= bp->attr & AttrReverse ? bp->attr&3:0;
  68.             goto    __Cont;
  69.         }else if( *ptr == Ctrl('T') ){
  70.             s_strcpych( '\r', bp->title, ptr+1 );
  71.             goto    __Cont;
  72.         }else if( *ptr == Ctrl('P') ){
  73.             bp->x= nn_numstr( ptr+1 );
  74.             ptr= skipnum( ptr+1 );
  75.             bp->y= nn_numstr( ptr+1 );
  76.             ptr= skipnum( ptr+1 );
  77.             if( *ptr == ',' ){
  78.                 int    rnd= nn_numstr( ptr+1 );
  79.                 bp->x+= RAND()%rnd;
  80.                 bp->y+= RAND()%rnd;
  81.             }
  82.             goto    __Cont;
  83.         }else if( *ptr == Ctrl('W') ){
  84.             bp->h= nn_numstr( ptr+1 );
  85.             bp->v= nn_numstr( s_strchr( ',', ptr+1 )+1 );
  86.             goto    __Cont;
  87.         }else if( *ptr == Ctrl('_') ){
  88.             if( ptr[1] < ' ' ){
  89.                 attr= 9;
  90.                 font= 12;
  91.                 option= 0;
  92.             }else{
  93.                 font= nn_numstr( ptr+1 );
  94.                 ptr= skipnum( ptr+1 );
  95.                 if( *ptr == ',' ){
  96.                     attr= nn_numstr( ptr+1 );
  97.                     ptr= skipnum( ptr+1 );
  98.                     if( *ptr == ',' )
  99.                         option= nn_numstr( ptr+1 );
  100.                 }
  101.             }
  102.         __Cont:
  103.             for(; *ptr++ != '\n' ;);
  104.             continue;
  105.         }
  106.         *lp++= str;
  107.         *str++= font;
  108.         *str++= option +1;
  109.         *str++= attr;
  110.         for( col= 0 ; col < bp->maxcol ;){
  111.             if( *ptr < ' ' ){
  112.                 if( *ptr == '\t' ){
  113.                     int l= bp->tabsize-(col&bp->tabmask);
  114.                     for( ptr++,col+=l; l-- ; *str++= ' ');
  115.                     continue;
  116.                 }else if( *ptr == '\n' ){
  117.                     ptr++;
  118.                     break;
  119.                 }else if( *ptr == '\r' ){
  120.                 ___cr:
  121.                     if( ptr[1] == '\n' )
  122.                         ptr++;
  123.                     ptr++;
  124.                     break;
  125.                 }else if( *ptr == Ctrl('X') ){
  126.                     *str++= '\0';
  127.                     ptr++;
  128.                     for(; *ptr != '\r' ; *str++= *ptr++ );
  129.                     goto    ___cr;
  130.                 }
  131.             }
  132.             kanji= kanji ? 0 : _KanjiC[*ptr];
  133.             *str++= *ptr++;
  134.             col++;
  135.         }
  136.         if( kanji ){
  137.             str--;
  138.             ptr--;
  139.         }
  140.         *str++= '\0';
  141.         line++;
  142.     }
  143.     bp->line= line;
  144.     bp->curpos= -1;
  145.     return    str;
  146. }
  147.  
  148. static
  149. FileLoad( fn, bp)
  150. T_BUFP    *bp;
  151. {
  152.     int    fsize= SEEK( fn, 0, 2 );
  153.     SEEK( fn, 0, 0 );
  154.     if( (int)(bp->lp= MALLOC( fsize+FOFF )) < 0 ){
  155.         CLOSE( fn );
  156.         Print( "tvv:alloc err\r\n" );
  157.         return    0;
  158.     }
  159.     READ( fn, (char*)(bp->lp)+FOFF, fsize );
  160.     CLOSE( fn );
  161.     if( (int)(bp->fbuf= MALLOC( fsize*2 )) < 0 ){
  162.         Print( "tvv:alloc err\r\n" );
  163.         return    0;
  164.     }
  165.     fsize= MakeBuf( bp, fsize )-bp->fbuf;
  166.     SETBLOCK( bp->fbuf, fsize );
  167.     SETBLOCK( bp->lp, sizeof(char*)*bp->line );
  168.     return    fsize;
  169. }
  170.  
  171. ReadFile( fname, bp, path )
  172. char    *fname, *path;
  173. T_BUFP    *bp;
  174. {
  175.     int    fn;
  176.     unsigned char    buf[98],
  177.             *p= path;
  178.     if( *fname == '/' || fname[1] == ':' ){
  179.         if( (fn= OPEN( fname, 0 )) >= 0 )
  180.             return    FileLoad( fn, bp );
  181.     }
  182.     for(; *p ; p++ ){
  183.         p= p_strcpych( ';', buf, p );
  184.         s_strcpy( s_strpath( buf ), fname );
  185.         if( (fn= OPEN( buf, 0 )) >= 0 )
  186.             return    FileLoad( fn, bp );
  187.     }
  188.     Print( fname );
  189.     Print( ":tvv open err\r\n" );
  190.     return    0;
  191. }
  192.  
  193.